<template>
  <div class="app-container">
    <el-card class="operate-container" shadow="never">
      <i class="el-icon-tickets" style="margin-top: 5px" />
      <span style="margin-top: 5px">店铺信息</span>
      <el-button
        class="btn-add"
        type="primary"
        size="mini"
        style="float: right"
        @click="handleAddProductCate()"
      >
        添加设置
      </el-button>
    </el-card>
    <div class="table-container">
      <el-table
        ref="merchantConfigTable"
        v-loading="listLoading"
        style="width: 100%"
        :data="list"
        fit
        border
      >
        <el-table-column label="编号" width="60" align="center">
          <template slot-scope="scope">{{ scope.row.id }}</template>
        </el-table-column>

        <el-table-column label="店铺名称" align="center" width="100">
          <template slot-scope="scope">{{ scope.row.shopName }}</template>
        </el-table-column>

        <el-table-column label="手机号码" width="160" align="center">
          <template slot-scope="scope">{{ scope.row.phone }}</template>
        </el-table-column>

        <el-table-column label="店铺LOGO" width="220" align="center">
          <template slot-scope="scope"><img style="height: 100px" :src="scope.row.logo"></template>
        </el-table-column>

        <el-table-column label="店铺地址" width="280" align="center">
          <template slot-scope="scope">{{ scope.row.address }}</template>
        </el-table-column>

        <el-table-column label="营业时间" width="200" align="center">
          <template slot-scope="scope">{{ scope.row.openTime }}</template>
        </el-table-column>

        <el-table-column label="店铺说明" width="338" align="center">
          <template slot-scope="scope">{{ scope.row.notes }}</template>
        </el-table-column>

        <el-table-column label="营业状态" width="80" align="center">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.status"
              :active-value="0"
              :inactive-value="1"
              @change="handleShowStatusChange(scope.$index, scope.row)"
            />
          </template>
        </el-table-column>


        <el-table-column label="操作" width="200" align="center">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="primary"
              @click="handleUpdate(scope.$index, scope.row)"
            >修改
            </el-button>
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)"
            > 删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>

<script>
    import { fetchList, deleteMerchantConfig, updateStatus } from '@/api/system/merchantConfig'

    const defaultStatus = {
        id: '',
        status: ''
    };
    export default {
        name: "MerchantConfigList",
        data() {
            return {
                list: null,
                listLoading: false,
                updateMerchantConfigStatus: Object.assign({}, defaultStatus)
            }
        },
        created() {
            this.getList();
        },
        // watch: {
        //     $route(route) {
        //         console.log(route)
        //         this.getList();
        //     }
        // },
        methods: {

            handleAddProductCate() {
                this.$router.push('/system/config/addMerchantConfig');
            },
            getList() {
                this.listLoading = true;
                fetchList().then(response => {
                    console.log(response)
                    this.listLoading = false;
                    this.list = response.data;
                });
            },
            handleShowStatusChange(index, row) {
                console.log(row)
                this.updateMerchantConfigStatus.id = row.id;
                this.updateMerchantConfigStatus.status = row.status;
                updateStatus(this.updateMerchantConfigStatus).then(response => {
                    this.$message({
                        message: '修改店铺设置成功',
                        type: 'success',
                        duration: 1000
                    });
                });
            },
            handleUpdate(index, row) {
                this.$router.push({ path: '/system/config/updateMerchantConfig', query: { id: row.id }});
            },
            handleDelete(index, row) {
                this.$confirm('是否要删除该品牌', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    deleteMerchantConfig(row.id).then(response => {
                        this.$message({
                            message: '删除店铺设置成功',
                            type: 'success',
                            duration: 1000
                        });
                        this.getList();
                    });
                });
            }
        }
    }
</script>

<style scoped>

</style>
